extends TextureButton


func _r2(aitem):
	if aitem == null :return
	if aitem.id == "eqp_skillBook":
		reRollSkillBook(aitem)
		return
	var g3 = 80 * pow(3,aitem.lv-1) * 0.2
	var inx2 = yield(sys.eventDlg.selBB([tr("支付 %d 金币") % g3,tr("放弃")]),"onSel")
	if inx2 == 0 :
		if sys.player.subItem("m_gold",g3) == false: 
			sys.eventDlg.txt(tr("金币 不足"))
			return
	else:
		return
	var item = data.newBase(aitem.id)
	item.create(aitem.lv)
	sys.player.items.delItem(aitem)
	var itemPck = ItemPck.new()
	itemPck.addItem(item)
	var eventItem = sys.eventDlg.items(itemPck)
	

	var utils = sys.get_node("VEBase").utils
	var btn = utils.createItemButton(eventItem, self, "继续随机", "_r2ex", [item, eventItem], Vector2(559, 36))
	btn.rect_size =  eventItem.get_node("Button").rect_size

func _r2ex(aitem, eventItem):
	eventItem._pressed()
	_r2(aitem)
	
func _on_eqpUp_pressed():
	var options = [tr("升级装备（需要宝石）"),tr("重新随机一个装备的词条"),tr("一键将背包中全部蓝装合成紫装，产出完全随机")]
	var vedoFlag = sys.has_node("VEDO")
	if vedoFlag:
		options.append("指定角色还原到1级，遗忘所有技能，返还技能点")
	options.append(tr("离开"))

	var inx = yield(sys.eventDlg.selBB(options),"onSel")
	if inx == 0 :
#		if n1 <= 0 :
#			sys.eventDlg.txt("次数 不足")
#			return
		yield(sys.eventDlg.selBB([tr("选择一个装备")]),"onSel")
		var dlg = sys.newDlg("res://!ve/additional/tiejiang_rewrite/selItemDlg.tscn")
		var items = sys.get_node("VEBase").utils.getPlayerTeamItemPck()
		dlg.init(preHandleItems(items),1,null,false)
		yield(dlg,"onDel")
		var eqp :Eqp = dlg.selItem
		if eqp == null: return	
		yield(sys.eventDlg.selBB([tr("选择一颗比装备高一级的宝石")]),"onSel")
		dlg = sys.newDlg("res://tscn/item/selItemDlg.tscn")
		dlg.init(sys.player.items,4)
		yield(dlg,"onDel")
		if dlg.selItem == null :return
		var gem = dlg.selItem
		if gem.lv - eqp.lv != 1 :
			sys.eventDlg.txt(tr("宝石必须比装备高一级"))
			return
		var g2 = int(100 * pow(3,eqp.lv-1) * 0.5)
		var inx2 = yield(sys.eventDlg.selBB([tr("支付 %d 金币") % g2,tr("放弃")]),"onSel")
		if inx2 == 0 : 
			if sys.player.subItem("m_gold",g2) == false :
				sys.eventDlg.txt(tr("金币 不足"))
				return
		else:
			return
			
		var eqpBox = null
		for i in $"../eqp/ScrollContainer/box".get_children():
			if i.item == eqp:
				eqpBox = i
				break
		var chaV = null
		
		sys.player.items.delItem(gem)
		eqp.plusLv(gem)
		var itemPck = ItemPck.new()
		itemPck.addItem(eqp)
		sys.eventDlg.itemShow(itemPck)
		
		if eqpBox != null:
			eqpBox.init(eqp)
			
		if eqp.wearer != null:
			var idx = 0
			for i in eqp.wearer.eqps.items:
				if i == eqp:
					break
				idx += 1
			eqp.wearer.eqps.up(idx, eqp, eqp)

		
	elif inx == 1 :
		var dlg = sys.newDlg("res://tscn/item/selItemDlg.tscn")
		dlg.init(sys.player.items,1,null,false)
		dlg.connect("onSel",self,"_r2")
	elif inx == 2:
		autoMergeEqp()
	elif inx == 3 and vedoFlag:
		forgetCha()

func forgetCha():
	sys.eventDlg.txt(tr("使你的一个单位遗忘所有回到1级，返还80%魂值"),true)
	var inx = yield(sys.eventDlg.selBB([tr("选择一个单位"),tr("离开")]),"onSel")
	if inx == 0 :
		var dlg = sys.newDlg("res://tscn/chara/selChaDlg.tscn")
		dlg.init(sys.player.team)
		dlg.connect("onSel",self,"doForgetCha")

func doForgetCha(cha:Chara):
	if cha.lv < 2:
		sys.newMsg("该角色等级过低！")
		return
	if cha.id.find("vebook") > -1:
		sys.newMsg("暂不支持该角色！")
		return
	var h = 0
	for i in cha.lv - 1:
		h += cha.baseXp * pow(3,i)
	var sp = 0
	sp -= (cha.lv - 1) * cha.spStup
	cha.lv = 1
	cha.upMaxXp()
	for i in range(cha.skills.items.size()-1,-1,-1):
		var k = cha.skills.items[i]
		sp += (k.lv - 1)
		if cha.skillIds.has(k.id):
			k.lv = 1
		else:
			cha.skills.items.remove(i)
	for i in cha.exSkills.items:
		i.lv = 1
		cha.skills.addItem(i)
	cha.sp += sp	
	var itemPck = ItemPck.new()
	itemPck.addItem(data.newBase("m_xp").setNum(h*0.8))
	sys.eventDlg.txt(tr("%s 已经遗忘所学技能") % tr(cha.name),true)
	sys.eventDlg.items(itemPck)


func autoMergeEqp():
	var its = []
	var itemPck = ItemPck.new()
	for j in range(sys.player.items.items.size()-1,-1,-1):
		var i = sys.player.items.items[j]
		if i.lv == 2 and i is Eqp:
			its.append(i)
		if its.size() >= 3:
			for k in its:
				sys.player.items.delItem(k)
			its.clear()
			var item = data.newBase(rnp.eqpPool.rndItem().id)
			item.create(3)
			itemPck.addItem(item)
	sys.eventDlg.items(itemPck)

func preHandleItems(oldPck):
	var pck = ItemPck.new()
	for i in oldPck.items:
		if i.id != "eqp_skillBook":
			pck.addItem(i)
	return pck

func reRollSkillBook(aitem):
	var g3 = 200
	var inx2 = yield(sys.eventDlg.selBB([tr("支付 %d 金币") % g3,tr("放弃")]),"onSel")
	if inx2 == 0 :
		if sys.player.subItem("m_gold",g3) == false: 
			sys.eventDlg.txt(tr("金币 不足"))
			return
	else:
		return
	var item = data.newBase(aitem.id)
	# item.create(aitem.lv)
	sys.player.items.delItem(aitem)
	var itemPck = ItemPck.new()
	itemPck.addItem(item)
	var eventItem = sys.eventDlg.items(itemPck)
	var utils = sys.get_node("VEBase").utils
	var btn = utils.createItemButton(eventItem, self, "继续随机", "_r2ex", [item, eventItem], Vector2(559, 36))
	btn.rect_size =  eventItem.get_node("Button").rect_size
